package com.itWaiLong.Service.Impl;

import com.itWaiLong.AppException.AppException;
import com.itWaiLong.AppException.GlobalExceptionHandler;
import com.itWaiLong.Dao.group_project;
import com.itWaiLong.Dao.personal_project;
import com.itWaiLong.Dao.project_share;
import com.itWaiLong.Mapper.GroupProjectMapper;
import com.itWaiLong.Mapper.PersonalProjectMapper;
import com.itWaiLong.Mapper.ShareProjectMapper;
import com.itWaiLong.Service.ShareProjectService;
import com.itWaiLong.Utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ShareProjectServiceImpl implements ShareProjectService {

    @Autowired
    private GroupProjectMapper groupProjectMapper;

    @Autowired
    private PersonalProjectMapper personalProjectMapper;

    @Autowired
    private ShareProjectMapper shareProjectMapper;

    /**
     * 用户分享自己的项目
     * @return
     */
    public void share(project_share projectShare) {

        //获取用户的id
        Map<String,Object> map = ThreadLocalUtil.getThreadLocal();
        int userId = (int) map.get("userId");
        projectShare.setUserId(userId);

        //先查看所分享的项目是否存在
        personal_project personalProject = personalProjectMapper.findProjectById(projectShare.getProjectId());
        if (personalProject == null) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }

        //判断该项目是否已被分享
        personal_project personalProject1 = shareProjectMapper.selectProjectById(projectShare.getProjectId());
        if (personalProject1 != null) {
            throw new GlobalExceptionHandler(AppException.XIANG_MU_YI_CUN_ZAI);
        }
        int result = shareProjectMapper.share(projectShare);
        if (result < 0) {
            throw new GlobalExceptionHandler(AppException.TIAN_JIA_CHENG_GONG);
        }
    }

    /**
     * 用户浏览分享页
     * @return
     */
    public List<project_share> browse() {
        List<project_share> projectShares = shareProjectMapper.browse();
        if (projectShares == null || projectShares.size() == 0) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_REN_HE_XIANG_MU);
        }
        return projectShares;
    }

    @Override
    public void delete(int projectId) {
        int result = shareProjectMapper.delete(projectId);
        if (result < 0) {
            throw new GlobalExceptionHandler(AppException.SHAN_CHU_SHI_BAI);
        }
    }

    /**
     * 分享团队的项目
     * @param projectShare
     * @return
     */
    public void groupShare(project_share projectShare) {

        //查询团队是否存在该项目
        group_project groupProject = groupProjectMapper.selectProjectById(projectShare.getProjectId());

        //判断项目是否存在
        if (groupProject == null) {
            //不存在
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }
        //存在
        //判断用户权限
        Map<String, Object> map = ThreadLocalUtil.getThreadLocal();
        int userId = (int) map.get("userId");
        if (userId != groupProject.getLeaderId()) {
            //权限不够
            throw new GlobalExceptionHandler(AppException.QUAN_XIAN_BU_CU);
        }
        //拥有权限
        projectShare.setUserId(userId);
        int result = shareProjectMapper.share(projectShare);
        if (result < 0) {
            throw new GlobalExceptionHandler(AppException.TIAN_JIA_CHENG_GONG);
        }
    }
}
